package com.srwl.mytx.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.srwl.mytx.R;
import com.srwl.mytx.domain.Present;
import com.srwl.mytx.widget.PresentDialog;

import java.util.List;

public class PresentRecyclerViewAdapter extends RecyclerView.Adapter<PresentRecyclerViewAdapter.MyViewHolder>{
    private static final String TAG = "PresentRecyclerViewAdap";
    private List<Present> datas;
    private LayoutInflater inflater;
    private OnItemClickListener mOnItemClickListener;
    private PresentDialog.InteractionListener mListener;
    private String liveRecordId;
    private String liveRoomId;
    private int audienceSize;
    private Context context;




    public PresentRecyclerViewAdapter(Context context,List<Present> datas){
        this.datas=datas;
        this.context =context;
        inflater = LayoutInflater.from(context);
    }
    public PresentRecyclerViewAdapter(Context context, String liveRecordid, String liveRoom, int accountListSize, PresentDialog.InteractionListener listerner, List<Present> datas){

        this.datas=datas;
        inflater = LayoutInflater.from(context);
        this.liveRecordId = liveRecordid;
        this.liveRoomId = liveRoom;
        this.audienceSize = accountListSize;
        this.mListener = listerner;
    }
    public class MyViewHolder extends RecyclerView.ViewHolder{
        private ImageView imageView;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            //textView =itemView.findViewById(R.id.rv_main_item_title);
            imageView = itemView.findViewById(R.id.p_images);
        }
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        // Log.e(TAG,"create a new item") ;
        MyViewHolder holder = new MyViewHolder(inflater.inflate(R.layout.rv_main_item,viewGroup,false));
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, int i) {
        /**
         * 为RecyclerView添加本地礼物图片
         */
        myViewHolder.imageView.setImageResource(datas.get(i).getImage());
        /**
         * 为RecyclerView添加从服务端获取的图片
         */
   /*    // GlideApp.with(context).load(datas.get(i).getPicture()).into(myViewHolder.imageView);


        RequestOptions options = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL);
//        Glide.with(context).asBitmap().load(datas.get(i).getPicture()).apply(options).into(myViewHolder.imageView);

        Glide.with(context).asBitmap().load(datas.get(i).getPicture()).apply(options).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull final Bitmap resource, @Nullable Transition<? super Bitmap> transition) {

                final float imgWidth = resource.getWidth();
                final float imgHeight = resource.getHeight();
                int width = (int) (imgWidth*1.9);
                int height = (int) (imgHeight*1.9);
                ViewGroup.LayoutParams params = myViewHolder.imageView.getLayoutParams();
                   params.width = width;
                   params.height = height;

                myViewHolder.imageView.setLayoutParams(params);
                myViewHolder.imageView.setImageBitmap(resource);
//                holder.image.setLayoutParams(fitImage(screenWidth, holder.image, imgWidth, imgHeight));
//                holder.image.setImageBitmap(resource);

                Log.d(TAG, "onResourceReady: 图片加载完成position:"  + "imgWidth:" + imgWidth + " imgHeight   " + imgHeight);

            }
        });*/

       /* int height = Math.abs((new Random().nextInt()) % 300);
        if (height < 200) {
            height += 200;
        }
        myViewHolder.textView.setHeight(height);*/

        // 设置事件响应

            myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemClickListener != null) {
                        Present present = datas.get(i);
                        mOnItemClickListener.onClick(present);
                    }
                }
            });






    }

  /*  public void addData(int position,String content){
        datas.add(position,content);
    }

    public void removeData(int position){
        datas.remove(position);
    }*/

    @Override
    public int getItemCount() {
        return datas.size();
    }
    public interface  OnItemClickListener{
        void onClick(Present present);
    }



    public void  setOnItemClickListent(OnItemClickListener listener){
        this.mOnItemClickListener = listener;
    }

}